package com.example.myvideoplayer.view;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myvideoplayer.R;

public class MainActivity extends AppCompatActivity {
    private TextView time;
    private final static int CHANGE_TIME = 0;
    private int totalTime = 5;
    private boolean isFinish = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == CHANGE_TIME) {
                if (time != null) {
                    time.setText(Integer.toString(totalTime));
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.
                permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new
                    String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    }

    private void init() {
        time = findViewById(R.id.tip);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isFinish && totalTime >= 0) {
                    handler.sendEmptyMessage(CHANGE_TIME);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    totalTime--;
                }
                if (!isFinish) {
                    jumpToPlayActivity();
                }
            }
        }).start();
    }

    private void jumpToPlayActivity() {
        finish();
        isFinish = true;
        Intent i = new Intent(this, VideoList.class);
        startActivity(i);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "已取得授权", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

    public void jump(View view) {
        jumpToPlayActivity();
    }

}
